package com.guess.lolguessing.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.guess.lolguessing.domain.dto.UserLoginDTO;
import com.guess.lolguessing.domain.po.KpUser;
import com.guess.lolguessing.domain.vo.LoginResponseVO;
import com.guess.lolguessing.porperties.JwtProperties;
import com.guess.lolguessing.result.Result;
import com.guess.lolguessing.service.impl.KpUserServiceImpl;
import com.guess.lolguessing.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080", "http://172.24.0.1:8080"})
public class KpUserController {
    private final KpUserServiceImpl userService;
    private final JwtProperties jwtProperties;
//    private final StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    public Result userLogin(@RequestBody UserLoginDTO dto, HttpSession session) {
        KpUser user = userService.userLogin(dto);
        // 生成jwt令牌
        Map<String, Object> maps = new HashMap<>();
        maps.put("userId", user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getKey(), jwtProperties.getTtl(), maps);
//
//        Map<String, Object> userMap = BeanUtil.beanToMap(dto, new HashMap<>(),
//                CopyOptions.create()
//                        .setIgnoreNullValue(true)
//                        .setFieldValueEditor((fieldName, fieldValue) -> fieldValue.toString()));
//        String tokenKey = "kp:login:" + token;
////        stringRedisTemplate.opsForHash().putAll(tokenKey, userMap);
////        // 7.4.设置token有效期
//        stringRedisTemplate.expire(tokenKey, jwtProperties.getTtl(), TimeUnit.MINUTES);
        return Result.success(token);
    }
}
